/*
 * TMap Designer - A Thematic Map design software
 * Copyright (C) 2006 World Health Organization
 * http://www.who.int
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 */
package who.tmap.toolkit.dao;

import java.awt.geom.Point2D;
import java.io.File;

import javax.swing.ImageIcon;

import org.jdom.Element;

import who.tmap.dao.TMapDAOException;
import who.tmap.toolkit.shape.MapShape;
import who.tmap.toolkit.shape.MapShapes;
import who.tmap.toolkit.shape.annotation.ImageShape;

/**
 * A DAO for image annotations
 * @author stl
 */
public class ImageShapeDAO extends BaseMapShapeDAO {

	public MapShape load( Element elt ) throws TMapDAOException {
	    ImageShape imageShape =null;
	    
        try {
            imageShape = (ImageShape)msf.getGraphic( MapShapes.IMAGE );
            
            Point2D pt =new Point2D.Double( 
                    			elt.getAttribute( "x" ).getDoubleValue(),
                    			-elt.getAttribute( "y" ).getDoubleValue() );
            imageShape.setPt( pt );
            
            imageShape.setWidth( elt.getAttribute( "width" ).getDoubleValue() );
            imageShape.setHeight( elt.getAttribute( "height" ).getDoubleValue() );
            
            imageShape.setFile( new File( elt.getAttributeValue( "fileName") ) );            
            
            //imageShape.loadFile( new File( elt.getChildText( "File" ) ) );
    		byte[] imageData =miscHelper.loadInlineImage( elt.getChild( "Inline" ) );
    		
    		imageShape.setImageBytes( imageData );
        	ImageIcon imageIcon =new ImageIcon( imageData );
        	imageShape.setImage( imageIcon.getImage() );


        } catch (Exception e) {
            throw new TMapDAOException( e );
        }
        return imageShape;
	}
	
	public Element save( MapShape mapShape ) throws TMapDAOException {
	    
        ImageShape imageShape =(ImageShape)mapShape;

		Element shapeElt =new Element( "Image" );
        
        shapeElt.setAttribute( "x", Double.toString( imageShape.getPt().getX() ) );
        shapeElt.setAttribute( "y", Double.toString( -imageShape.getPt().getY() ) );
        shapeElt.setAttribute( "width", String.valueOf( imageShape.getWidth() ) );
        shapeElt.setAttribute( "height", String.valueOf( imageShape.getHeight() ) );
        shapeElt.setAttribute( "fileName", imageShape.getFile().toString() );        
        
        Element inlineElt =new Element( "Inline" );
        shapeElt.addContent( inlineElt );
        inlineElt.setText( miscHelper.saveInlineImage( imageShape.getImageBytes() ) );
        
        return shapeElt;
	}
    
}
